package com.singfee.gisservice.controller;


import com.singfee.gisservice.entity.AjaxResult;
import com.singfee.gisservice.entity.Render;
import com.singfee.gisservice.service.IRenderServer;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/theme")
public class RenderController {

    @Autowired
    IRenderServer renderServer;

    @GetMapping("/list")
    public AjaxResult getLayerRenderList(String layerName)
    {
        Render render =new Render();
        if(StringUtils.isNotEmpty(layerName)) {
            render.setLayerName(layerName);
        }
        List<Map> list= renderServer.selectRenderList();
        return AjaxResult.success("请求成功",list);
    }

    @GetMapping("/query")
    public AjaxResult getLayerRender(@RequestParam String filter)
    {
        try {
            Render render = new Render();
            Boolean contentField = false;
            if (StringUtils.isNotEmpty(filter)) {
                String[] tempString = filter.split(",");
                for (String fieldValue : tempString) {
                    String[] fv = fieldValue.split(":");
                    String field = fv[0].toLowerCase();
                    String value = fv[1];
                    if (field.equals("project")) {
                        render.setProject(value);
                        contentField=true;
                    }
                    if (field.equals("themeName")) {
                        render.setThemeName(value);
                        contentField=true;
                    }
                    if (field.equals("tag")) {
                        render.setTag(value);
                        contentField=true;
                    }
                    if(field.equals("layername"))
                    {
                        render.setLayerName(value);
                        contentField=true;
                    }
                }
                if (contentField) {
                    List<Map> renderJson = renderServer.selectLayerRender(render);
                    return AjaxResult.success("请求成功", renderJson);
                } else {
                    return AjaxResult.warn("请求成功，但请至少回传一个有效参数。");
                }
            } else {
                return AjaxResult.error("请求成功，没有有效参数。");
            }
        }catch(Exception ex)
        {
            return AjaxResult.error("系统异常",ex.getMessage());
        }
    }

    @GetMapping("/add")
    public AjaxResult insertRender(@RequestParam Render render)
    {
        return AjaxResult.success("请求成功",renderServer.insertLayerRender(render)>0);
    }

    @GetMapping("/update")
    public AjaxResult updateRender(@RequestParam Render render)
    {
        return AjaxResult.success("请求成功",renderServer.updateLayerRender(render)>0);
    }

    @GetMapping("/deleteByIds")
    public AjaxResult deleteRenderById(@RequestParam Long[] ids) {
        try {
            if (ids.length > 0) {
                return AjaxResult.success("请求成功", renderServer.deleteRenderByIds(ids) > 0);
            } else {
                return AjaxResult.warn("id不存在", false);
            }
        } catch (Exception ex) {
            return AjaxResult.error(ex.getMessage());
        }
    }

    @GetMapping("/delById")
    public AjaxResult deleteRender(@RequestParam Long id)
    {
        return AjaxResult.success("请求成功",renderServer.delRenderById(id)>0);
    }

}
